package com.example.driptip.service;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;
import android.graphics.Color;
import android.view.WindowManager.LayoutParams;
import android.content.SharedPreferences;
import android.view.View;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.widget.Toast;

public class CountdownService extends Service {
    private WindowManager windowManager;
    private TextView countdownView;
    private CountDownTimer timer;

    @Override
    public void onCreate() {
        super.onCreate();
        createCountdownView();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (countdownView == null) {
            createCountdownView();
        }
        countdownView.setVisibility(View.VISIBLE);

        if (timer != null) {
            timer.cancel();
        }

        startCountdown(60000);
        return START_REDELIVER_INTENT;
    }

    private void startCountdown(long milliseconds) {
        if (countdownView != null) {
            countdownView.setVisibility(View.VISIBLE);
        }
        
        timer = new CountDownTimer(milliseconds, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (countdownView != null) {
                    long minutes = millisUntilFinished / (60 * 1000);
                    long seconds = (millisUntilFinished / 1000) % 60;
                    countdownView.setText(String.format("剩余时间: %02d:%02d", minutes, seconds));
                    countdownView.setVisibility(View.VISIBLE);  // 确保每次更新时视图都可见
                }
            }

            @Override
            public void onFinish() {
                if (countdownView != null) {
                    countdownView.setVisibility(View.GONE);
                }
                stopSelf();
            }
        }.start();
    }

    private void createCountdownView() {
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        
        countdownView = new TextView(this);
        countdownView.setTextColor(Color.WHITE);
        countdownView.setBackgroundColor(Color.parseColor("#80000000"));
        countdownView.setPadding(30, 10, 30, 10);
        countdownView.setTextSize(16);
        
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT
        );
        params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        params.y = 50;
        
        try {
            windowManager.addView(countdownView, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        
        if (windowManager != null && countdownView != null) {
            try {
                windowManager.removeView(countdownView);
            } catch (Exception e) {
                e.printStackTrace();
            }
            countdownView = null;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
} 